package com.situ.company.score.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.situ.company.department.model.DepartmentModel;
import com.situ.company.score.model.ScoreModel;
import com.situ.company.score.service.IScoreService;
import com.situ.company.score.service.impl.ScoreServiceImpl;
import com.situ.company.util.FmtRequest;
@WebServlet("/ScoreServlet")
public class ScoreServlet extends HttpServlet{

	private static final long serialVersionUID = 1L;
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}
	
	private IScoreService service = new ScoreServiceImpl();
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		Object res=null;
		switch(req.getParameter("action")) {
		case "page":
			res=page(req);
			break;
		case "list":
			res=list(req);
			break;
		case "add":
			res=add(req);
			break;
		case "del":
			res=del(req);
			break;
		case "get":
			res=get(req);
			break;
		case "upd":
			res=upd(req);
			break;
		}
		FmtRequest.write(resp.getWriter(), res);
	}

	private Object upd(HttpServletRequest req) {
		return service.update(FmtRequest.parseModel(req, ScoreModel.class));
	}

	private Object get(HttpServletRequest req) {
		return service.selectModel(FmtRequest.parseModel(req, ScoreModel.class));
	}

	private Object del(HttpServletRequest req) {
		return service.delete(FmtRequest.parseModel(req, ScoreModel.class));
	}

	private Object add(HttpServletRequest req) {
		return service.insert(FmtRequest.parseModel(req, ScoreModel.class));
	}

	private Object list(HttpServletRequest req) {
		return service.selectList(FmtRequest.parseModel(req, ScoreModel.class));
	}

	private Object page(HttpServletRequest req) {
		String codeEmp=req.getParameter("codeEmp");
		String codePro=req.getParameter("codePro");
		String pageIndex=req.getParameter("pageIndex");
		String pageLimit=req.getParameter("pageLimit");
		ScoreModel model=new ScoreModel();
		model.setCodeEmp(codeEmp);
		model.setCodePro(codePro);
		model.setPageIndex(Integer.parseInt(pageIndex));
		model.setPageLimit(Integer.parseInt(pageLimit));
		model.setPageOn(true);
		
		List<ScoreModel> list = service.selectList(model);//带有查询条件和分页条件，得到的当前页面要显示的数据记录
		//带有查询条件，不带有分页条件，得到的记录条数
		Integer count = service.selectCount(model);
		Map<String,Object> map=new HashMap<>();
		map.put("list", list);
		map.put("count", count);
		
		return map;
	}

}
